<?php
namespace User\Controller;

use Common\Controller\MemberbaseController;
class MycardController extends MemberbaseController{
	

	
	function _initialize() {
		parent::_initialize();
		$this->db = D("Card");
		$this->cate = D("CardCat");
		$this->log= D("GuanLog");
		$this->fenzhang= D("OrderFenzhang");

//		$this->myinfo=$this->cate->where(array("uid"=>UID))->find();

	}
	


	public  function  index(){

		$keyword=I("get.keyword");

		$search=false;
		if($keyword){
			$map["title"]=$keyword;

			$search=true;
			$this->assign("keyword",$keyword);
		}




		$map["catid"]=$this->myinfo["id"];


		$type=I("get.type");
		if($type){
			$map["type"]=$type;
			$search=true;

		}

		$is_use=I("get.is_use");
		if($is_use){
			if($is_use==1){
				$map["is_use"]=1;
			}
			if($is_use==2){
				$map["is_use"]=0;
			}

			$search=true;

		}
		$this->assign("search",$search);


		$count=$this->db->where($map)->count();
		$page = $this->page($count);
		$list=$this->db->limit($page->firstRow . ',' . $page->listRows)->order ( "id desc")->where($map)->select();

		$this->assign("Page",$page->show(""));



		$this->assign("list",$list);
		$this->display();




	}

	public  function  log(){


		$search=false;


		$starttime=strtotime(I('get.starttime'));

		$endtime=strtotime(I('get.endtime'));

		if($starttime||$endtime){

			$search=true;
			if(!$starttime){
				$this->error("请填写开始时间");

			}


			if(!$starttime){
				$this->error("请填写结束时间");
			}

			if($starttime>$endtime){
				$this->error("开始时间不能大于结束时间");



			}


			$map['inputtime']  = array('between',array($starttime,$endtime));

		}

		$map['j_id']  =$this->myinfo["id"];


		$this->assign("search",$search);

		$count=$this->log->where($map)->count();
		$page = $this->page($count);
		$list=$this->log->limit($page->firstRow . ',' . $page->listRows)->order ( "inputtime desc")->where($map)->select();

		foreach($list as $k=>$v){

			$info=M("Users")->where(array("open_id"=>$v["open_id"]))->find();
			$list[$k]["tel"]=$info["name"];
			$list[$k]["uid"]=$info["id"];





		}


		$this->assign("Page",$page->show(""));







		$this->assign("list",$list);
		$this->display();







	}
	public  function  xiangxi(){

		$search=false;

		$type=I("get.type")?I("get.type"):"tongji";

		$starttime=strtotime(I('get.starttime'));

		$endtime=strtotime(I('get.endtime'));


		if($starttime||$endtime){

			$search=true;
			if(!$starttime){
				$this->error("请填写开始时间");

			}


			if(!$starttime){
				$this->error("请填写结束时间");
			}

			if($starttime>$endtime){
				$this->error("开始时间不能大于结束时间");



			}


			$map['inputtime']  = array('between',array($starttime,$endtime));

		}




		$map['j_id']  =$this->myinfo["id"];



		$this->assign("search",$search);


		$sum = $this->fenzhang->where($map)->sum('money');
		$fen_sum = $this->fenzhang->where($map)->sum('fen_money');

			$count=$this->fenzhang->where($map)->count();
			$page = $this->page($count);
			$list=$this->fenzhang->limit($page->firstRow . ',' . $page->listRows)->order ( "inputtime desc")->where($map)->select();

			$this->assign("Page",$page->show(""));



			$this->assign("list",$list);
			$this->assign("count",$count);
			$this->assign("sum",$sum);
			$this->assign("fen_sum",$fen_sum);







		$this->display();





	}


	public function  daochu(){

		$starttime=strtotime(I('get.starttime'));

		$endtime=strtotime(I('get.endtime'));


		if($starttime||$endtime){

			$search=true;
			if(!$starttime){
				$this->error("请填写开始时间");

			}


			if(!$starttime){
				$this->error("请填写结束时间");
			}

			if($starttime>$endtime){
				$this->error("开始时间不能大于结束时间");



			}


			$map['inputtime']  = array('between',array($starttime,$endtime));

		}



		$map['j_id']  =I('get.id')?I('get.id'):$this->myinfo["id"];


		$xiangxi=$this->fenzhang->order ( "inputtime desc")->where($map)->select();

		import("Org.Util.PHPExcel");
		import("Org.Util.PHPExcel.Reader.Excel5");
		import("Org.Util.PHPExcel.Writer.Excel5");
		import("Org.Util.PHPExcel.IOFactory.php");



		$filename=EXCELS."/tongji_my.xls";


		$objPHPExcel=new \PHPExcel();
		$objPHPExcel=new \PHPExcel_Reader_Excel5();
		//载入文件

		$objPHPExcel=$objPHPExcel->load($filename);

		$objActSheet = $objPHPExcel->setActiveSheetIndex(0);

		//填入统计选项标题

		$column = 4;



		//填入标题
		$objActSheet->setCellValue('A1',"分销明细表");
		$objActSheet->setCellValue('A2',I('get.starttime')."----".I('get.endtime'));

//		//设置样式
//		$hsum=count(xiangxi)+4;
//		$styleArray = array(
//			'borders' => array(
//				'allborders' => array(
//
//					'style' => \PHPExcel_Style_Border::BORDER_THIN,//细边框
//
//				),
//			),
//			'alignment' => array(
//
//				'horizontal' => \PHPExcel_Style_Alignment::HORIZONTAL_CENTER
//
//			)
//		);
//
//		$objActSheet->getStyle('A4:G'.$hsum)->applyFromArray($styleArray);
		$objPHPExcel->getActiveSheet()->getDefaultRowDimension()->setRowHeight(20.25);


		//写入主体数据
		$s=0;
		foreach ($xiangxi as $k=>$v){
			$s++;;
			$objActSheet->setCellValueExplicit('A'.$column, $v["order_sn"],\PHPExcel_Cell_DataType::TYPE_STRING);


			if($v[modal]=="product"){
				$objActSheet->setCellValue('B'.$column, "保养");
			}
			if($v[modal]=="chongzhi"){

				$objActSheet->setCellValue('B'.$column, "充值");
			}

			$objActSheet->setCellValue('C'.$column, F("CardCat")[$v['j_id']]["name"]);



			$objActSheet->setCellValue('D'.$column, substr_replace(get_username($v["uid"]),'****',3,4) );



			$objActSheet->setCellValue('E'.$column, date("Y-m-d H:i:s",$v["inputtime"]));

			$objActSheet->setCellValue('F'.$column, $v["money"]);


			$objActSheet->setCellValue('G'.$column, $v["fen_money"]);







			$column++;

		}


		$filename=iconv('utf-8', 'gb2312', I('get.starttime')."-".I('get.endtime')."分销明细表.xls");
		header('Content-Type: application/vnd.ms-excel');
		header("Content-Disposition: attachment;filename=".$filename);
		header('Cache-Control: max-age=0');

		$objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
		$objWriter->save('php://output'); //文件通过浏览器下载
		exit;



	}


	public  function  qrcode(){





		$ticket=$this->u["ticket"];





		if(!$ticket){

			$code=$this->wk->qrcodeCreate($this->u["id"]);

			$ticket=$code["ticket"];
			$data=array(
				"id"=>$this->u["id"],
				"ticket"=>$code["ticket"]
			);
			D("Users")->save($data);



		}



		$code=$this->wk->showqrcode($ticket);

		$this->assign("code",$code);



			$this->display("");

	}


}